/**
 * [73] 矩阵置零
 * MDN 文档 String.prototype.replace()：https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace
 * @format
 * @lc app=leetcode.cn id=73 lang=javascript
 */

// @lc code=start
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
var setZeroes = function (matrix) {
    // 初始化二进制记录行列信息，某位为 0 表示对应行或列为 0
    // let row = (2 ** matrix.length - 1).toString(2)
    // let col = (2 ** matrix[0].length - 1).toString(2)
    let row = ""
    for (let m = 0; m < matrix.length; m++) row += "1"
    let col = ""
    for (let n = 0; n < matrix[0].length; n++) col += "1"
    // 字符串能通过 str[n] 索引访问但无法修改对应字符
    // 利用 str.substring() 方法封装字符串指定字符替换函数
    // js替换字符串中指定位置的字符：https://segmentfault.com/a/1190000022766423
    const strReplace = (s, i, c) => s.substring(0, i) + c + s.substring(i + 1, s.length)
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            if (matrix[i][j] === 0) (row = strReplace(row, i, "0")), (col = strReplace(col, j, "0"))
        }
    }
    for (let i = 0; i < matrix.length; i++) {
        for (let j = 0; j < matrix[0].length; j++) {
            if (row[i] == 0 || col[j] == 0) matrix[i][j] = 0
        }
    }
    // console.log(row, col)
}
// debugger
// const matrix = [
//     [1, 1, 1],
//     [1, 0, 1],
//     [1, 1, 1]
// ]
// setZeroes(matrix)
// console.log(matrix)
// @lc code=end
